<?php

namespace Agp\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Fnx\CoreBundle\Entity\Imagem
 *
 * @ORM\Table(name="imagem")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks()
 */
class Imagem
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="cod_imagem", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $cod_imagem;
    
    /**
     * @var string $caminho
     *
     * @ORM\Column(name="caminho", type="string", length=255)
     */
    protected $caminho;
    
    /**
     * @Assert\File(maxSize="6000000")
     */
    private $file;
    
    protected $filenameForRemove;
    
    protected $pathToMoveEcommerce = false;

    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
    public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;
        // check if we have an old image path
        if (isset($this->caminho)) {
            // store the old name to delete after the update
            $this->filenameForRemove = $this->caminho;
            $this->caminho = null;
        }

        $filename = sha1(uniqid(mt_rand(), true));
        $this->caminho = $filename.'.'.$this->getFile()->guessExtension();
    }
    
    /**
     * Get file.
     *
     * @return UploadedFile
     */
    public function getFile()
    {
        return $this->file;
    }
    
   /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->getFile()) {
            return;
        }
        
        $dir = $this->getUploadRootDir();
        if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }

        // if there is an error when moving the file, an exception will
        // be automatically thrown by move(). This will properly prevent
        // the entity from being persisted to the database on error
        $this->getFile()->move($this->getUploadRootDir(), $this->caminho);
        
        if ($this->pathToMoveEcommerce){
            
            $dir = $this->getUploadEcommerceRootDir();
            if (!file_exists($dir)) {
                mkdir($dir, 0777, true);
            }
            
            copy($this->getUploadRootDir().'/'.$this->caminho, $this->getUploadEcommerceRootDir().'/'.$this->caminho);
        }
        // check if we have an old image
        if (isset($this->filenameForRemove)) {
            // delete the old image
            if ($this->filenameForRemove != "" AND file_exists($this->getUploadRootDir().'/'.$this->filenameForRemove)){
                unlink($this->getUploadRootDir().'/'.$this->filenameForRemove);
            }
            
            if ($this->pathToMoveEcommerce != false AND $this->filenameForRemove != "" AND file_exists($this->getUploadEcommerceRootDir().'/'.$this->filenameForRemove)){
                unlink($this->getUploadEcommerceRootDir().'/'.$this->filenameForRemove);
            }
            
            // clear the temp image path
            $this->filenameForRemove = null;
        }
        
        $this->file = null;
    }
    
    /**
     * @ORM\PreRemove()
     */
    public function storeFilenameForRemove()
    {
        $this->filenameForRemove = $this->getAbsolutePath();
    }
    
   /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {   
        if (isset($this->filenameForRemove)) {
           unlink($this->filenameForRemove);
        }
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getCodImagem()
    {
        return $this->cod_imagem;
    }

    /**
     * Set caminho
     *
     * @param string $caminho
     */
    public function setCaminho($caminho)
    {
        $this->caminho = $caminho;
    }
    
    /**
     * Get caminho
     *
     * @return string 
     */
    public function getCaminho()
    {
        return $this->caminho;
    }
    
     public function getAbsolutePath()
    {
        return null === $this->caminho ? null : $this->getUploadRootDir().'/'.$this->caminho;
    }

    public function getWebPath()
    {
        return null === $this->caminho ? null : $this->getUploadDir().'/'.$this->caminho;
    }
    
    public function getUploadEcommerceRootDir(){
        return __DIR__.$this->pathToMoveEcommerce.$this->getUploadDir();
    }

    public function getUploadRootDir()
    {
        // the absolute directory path where uploaded documents should be saved
        return __DIR__.'/../../../../web/'.$this->getUploadDir();
    }

    public function getUploadDir()
    {
        // get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
        return 'uploads/images';
    }
    
    public function setPathToMoveEcommerce($pathToMoveEcommerce){
        $this->pathToMoveEcommerce = $pathToMoveEcommerce;
    }

}